<template>
    <view>
        <!-- pages/center/profile.wxml -->
        <!-- <header name="个人信息" type="1"></header> -->
        <view class="list" @tap="upimg">
            <text class="title">头像</text>
            <view>
                <image :src="inputdata.headimgurl?inputdata.headimgurl:'/static/headimgurl.png'" mode="aspectFill" class="headimgurl"></image>
                <image src="/static/next.png" mode="aspectFit"></image>
            </view>
        </view>
        <view class="list">
            <text class="title">用户名</text>
            <view>
                <input type="text" v-model="inputdata.nickname" disabled data-name="nickname" />
                <!-- <image src="/static/next.png" mode="aspectFit"></image> -->
            </view>
        </view>
        <view class="list">
            <text class="title">手机号</text>
            <view @tap="gophone">
                <input type="text" v-model="inputdata.phone" disabled data-name="name" />
                <!-- <image src="/static/next.png" mode="aspectFit"></image> -->
            </view>
        </view>
		<view class="list">
		    <text class="title">姓名</text>
		    <view>
		        <input type="text" v-model="inputdata.name" :disabled="oldname?true:false" data-name="name" />
		        <image src="/static/next.png" mode="aspectFit" v-if="!oldname?true:false"></image>
		    </view>
		</view>
		<view class="list">
		    <text class="title">身份证号</text>
		    <view>
		        <input type="text" v-model="inputdata.card" :disabled="oldcard?true:false" data-name="name" />
		        <image src="/static/next.png" mode="aspectFit" v-if="!oldcard?true:false"></image>
		    </view>
		</view>
        <!-- <view class="list">
    <text  class="title"> 手机号 </text>
    <view bindtap="gopage" data-url="/pages/center/profile/phone">
      <text>更改手机号</text>
      <image src="/static/next.png" mode="aspectFit"></image>
    </view>
  </view> -->
        <view class="btns" @tap="submit">保存</view>
    </view>
</template>

<script>
// pages/center/profile.js
export default {
    
    data() {
        return {
            inputdata: {
                headimgurl: '',
                nickname: '',
                phone: '',
				name:'',
				card:''
            },

            count: 0,
            isipx: '',
			uploadurl:'',
			oldname:'',
			oldcard:''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        // this.isipx= getApp().globalData.statusHeight
        that.init();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    // onShareAppMessage: function () {
    // }
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */ methods: {
        init() {
            var _this = this;

            _this.$http('center/getuser', {}, function (res) {
               _this.inputdata= res.data
			   _this.uploadurl = res.data.uploadurl;
			   _this.oldname = res.data.name;
			   _this.oldcard = res.data.card;
            });
        },


        gopage(e) {
            var url = e.currentTarget.dataset.url;
            uni.navigateTo({
                url: url
            });
        },

        upimg(e) {
            var _this = this;

            uni.chooseImage({
                count: 1,
                // 默认9
                sizeType: ['compressed'],
                // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'],
                // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    uni.showLoading({
                        title: '加载中'
                    });
                    var tempPath = res.tempFilePaths;
					const arrayBuffer = new Uint8Array(res.tempFilePaths[0]); //先将本地图片路径转换成array类型
					const base64Img = uni.arrayBufferToBase64(arrayBuffer);  //再转换成base64类型
					_this.inputdata.headimgurl = base64Img
					console.log(base64Img)
                }
            });
        },

        submit() {
            var _this = this;

            console.log(this.inputdata.headimgurl);

            if (typeof this.inputdata.headimgurl == 'object') {
                this.inputdata.headimgurl = this.inputdata.headimgurl[0];
                this.setData({
                    inputdata: this.inputdata
                });
            }

            if (this.count == 0) {
                _this.$http(
                    'center/upuser',
                    {
                        nickname: this.inputdata.nickname
                    },
                    function (res) {
                        if (res.code == 0) {
                            uni.showToast({
                                title: res.msg,

                                complete() {
                                    setTimeout(() => {
                                        uni.navigateBack();
                                    }, 1200);
                                }
                            });
                        } else {
                            uni.showToast({
                                title: res.msg
                            });
                        }
                    }
                );
            } else {
                app.globalData.http(
                    'app/center/upuser',
                    {
                        headimgurl: this.inputdata.headimgurl,
                        nickname: this.inputdata.nickname
                    },
                    function (res) {
                        if (res.code == 0) {
                            uni.showToast({
                                title: res.msg,

                                complete() {
                                    setTimeout(() => {
                                        uni.navigateBack();
                                    }, 1200);
                                }
                            });
                        } else {
                            uni.showToast({
                                title: res.msg
                            });
                        }
                    }
                );
            }
        },

        gophone() {
            uni.navigateTo({
                url: '/pages/center/newphone/index'
            });
        }
    }
};
</script>
<style lang="scss">
@import './profile.scss';
</style>
